<template>
  <div class="app-container">
    <!-- 列表 -->
    <el-table :data="lends" stripe>
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="lendNo" label="标的编号" width="160" />
      <el-table-column prop="amount" label="标的金额" />
      <el-table-column prop="period" label="投资期数" />
      <el-table-column label="年化利率">
        <template slot-scope="scope">
          {{ scope.row.lendYearRate * 100 }}%
        </template>
      </el-table-column>
      <el-table-column prop="investAmount" label="已投金额" />
      <el-table-column prop="investNum" label="投资人数" />
      <el-table-column prop="publishDate" label="发布时间" width="150" />
      <el-table-column prop="lendStartDate" label="开始日期" />
      <el-table-column prop="lendEndDate" label="结束日期" />
      <el-table-column prop="param.returnMethod" label="还款方式" />
      <el-table-column prop="param.status" label="状态" />

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini">
            <router-link :to="'/lend/info/' + scope.row.id">查看</router-link>
          </el-button>
          <el-button
            v-if="scope.row.status == 0"
            type="warning"
            size="mini"
            @click="handlePublish(scope.row)"
          >
            发布
          </el-button>
          <el-button
            v-if="scope.row.status == 1"
            type="warning"
            size="mini"
            @click="makeLoan(scope.row)"
          >
            放款
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import lendApi from '@/api/lend'
export default {
  data() {
    return {
      lends: [],
    }
  },
  created() {
    this.list()
  },
  methods: {
    handlePublish(row) {
      this.$confirm(`此操作将发布标的:${row.title} , 是否继续?`, '发布确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          //修改标的的状态为募资中
          lendApi.publish(row.id, 1).then((r) => {
            this.$message.success(`${row.title}标的发布成功`)
            this.list()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    makeLoan(row) {
      //提交放款请求
      this.$confirm(`此操作将发布放款:${row.title} , 是否继续?`, '发布确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          //修改标的的状态为募资中
          lendApi.makeLoan(row.id).then((r) => {
            this.$message.success('放款成功')
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    list() {
      lendApi.list().then((r) => {
        this.lends = r.data.items
      })
    },
  },
}
</script>
